package acc_dao

import (
	"github.com/jinzhu/gorm"
	"time"
)

type User struct {
	Id        int       `json:"id" gorm:"primary_key" description:"自增主键"`
	RoomId    int       `json:"room_id" gorm:"column:room_id" description:"房间ID"`
	Name      string    `json:"name" gorm:"column:name" description:"用户名称"`
	Token     string    `json:"token" gorm:"column:token" description:"用户token"`
	IsDelete  int       `json:"is_delete" gorm:"column:is_delete" description:"是否删除"`
	CreatedAt time.Time `json:"create_at" gorm:"column:create_at" description:"创建时间"`
}

func (u *User) TableName() string {
	return "chat_room_user"
}

/**
 * @Author: yang
 * @Description：查找聊天用戶--查询
 * @Date: 2021/10/26 11:23
 */
func (u *User) Find(tx *gorm.DB, search *User) (*User, error) {
	out := &User{}
	if err := tx.Where(search).Find(&out).Error; err != nil {
		return nil, err
	}
	return out, nil
}

/**
 * @Author: yang
 * @Description：获取用户列表
 * @Date: 2021/11/2 16:09
 */
func (u *User) List(tx *gorm.DB, search *User) (list []User, err error) {
	err = tx.Where(search).Find(&list).Error
	return
}

/**
 * @Author: yang
 * @Description：更新用户信息
 * @Date: 2021/11/3 14:48
 */
func (u *User) Save(tx *gorm.DB) error {
	if err := tx.Save(u).Error; err != nil {
		return err
	}
	return nil
}
